package com.sun.handler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 自定义数据类型插入到varchar类型的数据库中
 * 将Object类型的数据插入到varchar类型
 */

@MappedTypes(Object.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class DefinitionType2Varchar implements TypeHandler<Object> {

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
        preparedStatement.setObject(i,o);
    }

    @Override
    public Object getResult(ResultSet resultSet, String s) throws SQLException {
        Object object = resultSet.getObject(s);
        if(object != null){
            return object;
        }
        return null;
    }

    @Override
    public Object getResult(ResultSet resultSet, int i) throws SQLException {
        Object object = resultSet.getObject(i);
        if(object != null){
            return object;
        }
        return null;
    }

    @Override
    public Object getResult(CallableStatement callableStatement, int i) throws SQLException {
        Object object = callableStatement.getObject(i);
        if(object != null){
            return object;
        }
        return null;
    }
}
